package office.service.hr.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import office.FormulaParser;
import office.bo.hr.impl.Examine;
import office.bo.hr.impl.ExamineFormula;
import office.bo.hr.impl.ExamineManage;
import office.bo.hr.impl.ExamineProject;
import office.bo.hr.impl.ExamineProjectStandard;
import office.bo.hr.impl.ExamineSeting;
import office.bo.hr.impl.FormulaProject;
import office.dao.hr.ExamineDP;
import office.dao.hr.ExamineFormulaDP;
import office.dao.hr.ExamineManageDP;
import office.dao.hr.ExamineProjectDP;
import office.dao.hr.ExamineProjectStandardDP;
import office.dao.hr.ExamineSetingDP;
import office.dao.hr.FormulaProjectDP;
import office.domainset.BizDomainProviderSetImpl;
import office.service.hr.IExamineService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.dao.impl.DaoException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class ExamineService extends BaseBizServiceImpl implements IExamineService {
	
	@SuppressWarnings("unchecked")
	public List<ExamineManage> getAllExamineManage(Page page){
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("str", 1);
			List<ExamineManage> list = (List<ExamineManage>)getDP(ExamineManageDP.class).findByHqlNameAndDomian("allExamineManage", map, page);
			return list;
		} catch (Exception e) {
			log.error("", e);
			return null;
		} 
	}
	
	public void saveExamineManage(ExamineManage examineManage){
		try {
			getDP(ExamineManageDP.class).saveOrUpdate(examineManage);
		} catch (DaoException e) {
			log.error("", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExamineProject> getAllExamineProject(String ptype) {
		try {
			if(ptype == null){
				List<ExamineProject> list = (List<ExamineProject>)getDP(ExamineProjectDP.class).findByHqlNameAndDomian("AllExamineProject",new String[]{});
				return list;
			}
			String params[] = {ptype};
			List<ExamineProject> list = (List<ExamineProject>)getDP(ExamineProjectDP.class).findByHqlNameAndDomian("examineProjectByPtype",params);
			return list;
		} catch (Exception e) {
			log.error("", e);
			return null;
		} 
	}

	@Override
	public void saveExamineProject(ExamineProject examineProject) {
		try {
			getDP(ExamineProjectDP.class).saveOrUpdate(examineProject);
		} catch (DaoException e) {
			log.error("", e);
		}
	}
	
	@Override
	public void saveExamineProjectStandard(ExamineProjectStandard examineProjectStandard) {
		try {
			getDP(ExamineProjectStandardDP.class).saveOrUpdate(examineProjectStandard);
		} catch (DaoException e) {
			log.error("", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ExamineProjectStandard> getExamineProjectStandardByExamineProjectId(String examineProjectId) {
		String params[] = {examineProjectId};
		List<ExamineProjectStandard> list = null;
		try {
			list = (List<ExamineProjectStandard>)getDP(ExamineProjectStandardDP.class).findByHqlNameAndDomian("examineProjectStandardByExamineProjectId",params);
		} catch (Exception e) {
			log.error("", e);
		} 
		return list;
	}
	
	@Override
	public void deleteExamineProjectStandardById(String id) throws Exception {
		getDP(ExamineProjectStandardDP.class).deleteById(id);
	}
	
	@Override
	public void deleteExamineProjectStandardByExamineProjectId(String examineProjectId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("examineProjectId", examineProjectId);
		try {
			getDP(ExamineProjectStandardDP.class).exeByHqlNameAndDomian("deleteExamineProjectStandardByExamineProjectId", map);
		} catch (Exception e) {
			log.error("", e);
		}  
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<ExamineProject> getExamineProjectByRole(String roleName) {
		String params[] = {roleName};
		List<ExamineProject> list = null;
		try {
			list = (List<ExamineProject>)getDP(ExamineProjectDP.class).findByHqlNameAndDomian("examineProjectByRole",params);
		} catch (Exception e) {
			log.error("", e);
		} 
		return list;
	}

	@Override
	public Examine getExamine(String fromUserId, String toUserId, String eProjectId, String examineManagerId) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("toUserId", toUserId);
		map.put("examineManagerId", examineManagerId);
		try{
			if (fromUserId == null && eProjectId == null) {
				return (Examine)getDP(ExamineDP.class).findUniqueByHqlNameAndDomian("calculationTotal", map);
			} else {
				map.put("eProjectId", eProjectId);
				return (Examine)getDP(ExamineDP.class).findUniqueByHqlNameAndDomian("examine", map);
			}
		}catch (Exception e) {
			log.error("", e);
			return null;
		} 
	}

	@Override
	public ExamineManage getExamineManage(String id) {
		try {
			return (ExamineManage)getDP(ExamineManageDP.class).findByIdAndDomain(id);
		} catch (Exception e) {
			log.error("", e);
		} 
		return null;
	}

	@Override
	public void saveExamine(Examine ex) {
		try {
			getDP(ExamineDP.class).saveOrUpdate(ex);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Override
	public Double calculate(String userId,String examineManagerId) {
		try{
			ExamineFormula examineFormula = getExamineFormulaByUserId(userId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("toUserId", userId);
			map.put("examineManagerId", examineManagerId);
			if(examineFormula != null){
				String formula = examineFormula.getFormula();
				if(formula != null && !formula.equals("")){
					String strArray[] = formula.split("\\[");
					for (int i = 0; i < strArray.length; i++) {
						if(strArray[i] != null && !strArray[i].equals("")){
							String type = strArray[i].substring(0,strArray[i].indexOf("]"));
							if(type!=null &&!type.equals("")){
								map.put("type", type);
								Double d = (Double)getDP(ExamineDP.class).findUniqueByHqlNameAndDomian("sumScoreByToUseridTypeManager", map);
								int size = getDP(ExamineDP.class).findByHqlNameAndDomian("sumPeopleByExamine", map).size();
								Double result = newAverage(d,size);
								formula = formula.replace("["+type+"]",""+result);
							}
						}
					}
				}
				Double i = new Double(new FormulaParser(formula).getResult());
				return i;
			}
		}catch(Exception e){
			log.error("", e);
			return 0d;
		}
		return 0d;
	}
	
	private Double newAverage(Double d,int size){
		if(d == null){
			return 0.0;
		}else{
			return (d/size);
		}
	}
	
	@SuppressWarnings("unchecked")
	public Page getAllExamineManageTotalPage(){
		Page page = new Page();
		int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "common.pageSize"));
		page.setPageSize(pageSize);
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("str", 1);
			List<ExamineManage> list = (List<ExamineManage>)getDP(ExamineManageDP.class).findByHqlNameAndDomian("allExamineManage", map);
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			return page;
		} catch (Exception e) {
			log.error("", e);
			return null;
		} 
	}
	
	@Override
	public ExamineProject getExamineProjectById(String id) throws Exception {
		ExamineProject examineProject = (ExamineProject)getDP(ExamineProjectDP.class).findByIdAndDomain(id);
		return examineProject;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Examine> getExamineByUid(String uid,String examineManagerId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", uid);
		map.put("examineManagerId", examineManagerId);
		List<Examine> list = null;
		try {
			list = (List<Examine>)getDP(ExamineDP.class).findByHqlNameAndDomian("examineByUidAndManagerId", map);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}  
		return list;
	}
	
	@Override
	public void deleteExamineProjectById(String id) throws Exception {
		getDP(ExamineProjectDP.class).deleteById(id);
	}

	
	@Override
	public Examine getExamineById(String id) {
		try {
			return getDP(ExamineDP.class).findByIdAndDomain(id);
		} catch (Exception e) {
			log.error("", e);
			return null;
		} 
	}

	@Override
	public void saveExamineSeting(ExamineSeting examineSeting) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().saveOrUpdate(examineSeting);
			domainProviderSetImpl.getDao().getMySession().flush();
		} catch (DaoException e) {
			log.error("", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExamineSeting> getExamineSetingListByFromUserId(String fromUserId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fromUserId", fromUserId);
		List<ExamineSeting> list = null;
		try {
			list = (List<ExamineSeting>)getDP(ExamineSetingDP.class).findByHqlNameAndDomian("examineSetingListByFromUserId", map);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}  
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExamineSeting> getExamineSetingListByToUserId(String toUserId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("toUserId", toUserId);
		List<ExamineSeting> list = null;
		try {
			list = (List<ExamineSeting>)getDP(ExamineSetingDP.class).findByHqlNameAndDomian("examineSetingListByToUserId", map);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}  
		return list;
	}

	@Override
	public void deleteExamineSetingByFormUserId(String fromUserId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fromUserId", fromUserId);
		try {
			getDP(ExamineSetingDP.class).exeByHqlNameAndDomian("deleteExamineSetingByFormUserId", map);
		} catch (Exception e) {
			log.error("", e);
		}  
		
	}
	
	@Override
	public void deleteExamineSetingByToUserId(String toUserId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("toUserId", toUserId);
		try {
			getDP(ExamineSetingDP.class).exeByHqlNameAndDomian("deleteExamineSetingByToUserId", map);
		} catch (Exception e) {
			log.error("", e);
		}  
		
	}

	@Override
	public ExamineProjectStandard getExamineProjectStandardById(String id) {
		BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
		IDomainProvider examineProjectStandardDP = domainProviderSetImpl.getExamineProjectStandardDP();
		ExamineProjectStandard examineProjectStandard = null;
		try {
			examineProjectStandard = (ExamineProjectStandard)getDP(ExamineProjectStandardDP.class).findByIdAndDomain(examineProjectStandardDP, id);
		} catch (Exception e) {
			log.error("", e);
		} 
		return examineProjectStandard;
	}

	@Override
	public void saveExamineFormula(ExamineFormula examineFormula) {
		try {
			getDP(ExamineFormulaDP.class).saveOrUpdate(examineFormula);
		} catch (DaoException e) {
			log.error("", e);
		}
	}

	@Override
	public ExamineFormula getExamineFormulaByUserId(String userId) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("userId", userId);
		try {
			ExamineFormula examineFormula = (ExamineFormula)getDP(ExamineFormulaDP.class).findUniqueByHqlNameAndDomian("examineFormulaByUserId", map);
			return examineFormula;
		} catch (Exception e) {
			log.error("", e);
		} 
		return null;
	}

	/**
	 * 获取所有考核公式
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ExamineFormula> getAllExamineFormula() {
		try {
			List<ExamineFormula> list = getDP(ExamineFormulaDP.class).findAllByDomian();
			return list;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	@Override
	public FormulaProject getFormulaProjectByName(String name) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("name", name);
		try {
			return (FormulaProject)getDP(FormulaProjectDP.class).findUniqueByHqlNameAndDomian("formulaProjectByName", map);
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Examine> getExamineTotalByUid(String uid,String examineManagerId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", uid);
		map.put("examineManagerId", examineManagerId);
		List<Examine> list = null;
		try {
			list = (List<Examine>)getDP(ExamineDP.class).findByHqlNameAndDomian("examineTotalByUidAndManagerId", map);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}  
		return list;
	}

	@Override
	public void deleteExamine(String examineManagerId) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("examineManagerId", examineManagerId);
		getDP(ExamineDP.class).exeByHqlNameAndDomian("deleteExamineByManagerId", map);
	}

	@Override
	public void deleteExamineAndManager(String examineManagerId) {
		try {
			deleteExamine(examineManagerId);
			deleteExamineManager(examineManagerId);
		} catch (Exception e) {
			log.error("", e);
		} 
	}

	@Override
	public void deleteExamineManager(String examineManagerId) throws Exception  {
		try {
			getDP(ExamineManageDP.class).deleteById(examineManagerId);
		} catch (Exception e) {
			log.error("", e);
		}
	}

}
